var _VIEW_PATH = '../app_module/diy/views/';
var cartM = require('../models/cart.js');
module.exports = {
    index:function(req,res){
        var uid = req.AV.user.id;
        res.render(_VIEW_PATH+'cart/cart',{title:'自提商品购物车'});
    },
    //add ajax方法
    add : function(req, res){
        var uid = req.AV.user.id;
        var pid = req.body.pid;
        var psid = req.body.psid;
        var number = req.body.number;
        var price = req.body.price;
        cartM.add(uid,pid,psid,number,price).then(function(result){
            var jsonresult = {code: result.code, msg:result.objectId};
            res.json(jsonresult);
        }).catch(function(err){
            var jsonresult = {code: err.code, msg:err.msg};
            res.json(jsonresult);
        });
    },
    //5获取购物车商品数据列表
    getProductListAjax: function (req, res) {
        var uid = req.AV.user.id;
        var ProductArray = new Array();
        cartM.getProductList(uid).then(function (result) {
            var psid;
            var specArray;
            var imgArray;
            // console.log('控制器！！！！！！！');
            // console.log(result);
            if (result.rows > 0) {
                for (x in result.data) {

                    psid = result.data[x].get('psid'); //读取购物车单条数据的商品规格ID
                    specArray = result.data[x].get('product').get('spec'); //读取商品规格数组
                    imgArray = JSON.parse(result.data[x].get('product').get('picurlarray'));//读取商品图片数组

                    ProductArray.push(
                        {
                            //返回到前端的购物车商品数据
                            objectId: result.data[x].id,
                            pid:result.data[x].get('pid'),
                            cid:result.data[x].get('product').get('cid'),
                            name: result.data[x].get('product').get('name'),
                            specname: specArray[psid]['name'],
                            inventory:specArray[psid]['inventory'],
                            img: imgArray[0],
                            number: result.data[x].get('number'),
                            inputnumber: result.data[x].get('number'),
                            price:result.data[x].get('price'),
                            amountprice:result.data[x].get('amountprice'),
                            //mold: result.data[x].get('mold'),
                            checked: result.data[x].get('checked'),
                            uid: result.data[x].get('uid')
                        }
                    )
                }
            }
            var jsonresult = {code: result.code, rows: result.rows, ProductArray: ProductArray};
            res.json(jsonresult);
        });
    },
//删除购物车中的商品
    delCartProductAjax:function(req,res){
        var objectid=req.query.CartProductId;
        console.log(objectid);
        if(objectid){
            cartM.delCartProduct(objectid).then(function(result){
                var jsonresult = {code: result.code, msg:result.msg};
                res.json(jsonresult);
            }).catch(function(err){
                var jsonresult = {code: err.code, msg:err.reason};
                res.json(jsonresult);
            });
        }else{
            res.end('objectid is null');
        }
    },
}